package com.ruoyi.generator.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.generator.domain.FromBuildPage;
import com.ruoyi.generator.mapper.FromBuildPageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.generator.mapper.AppBuildInfoMapper;
import com.ruoyi.generator.domain.AppBuildInfo;
import com.ruoyi.generator.service.IAppBuildInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 移动端设计应用信息Service业务层处理
 *
 * @author ZGL
 * @date 2025-05-09
 */
@Service
public class AppBuildInfoServiceImpl implements IAppBuildInfoService
{
    @Autowired
    private AppBuildInfoMapper appBuildInfoMapper;

    @Autowired
    private FromBuildPageMapper fromBuildPageMapper;

    /**
     * 查询移动端设计应用信息
     *
     * @param id 移动端设计应用信息主键
     * @return 移动端设计应用信息
     */
    @Override
    public AppBuildInfo selectAppBuildInfoById(Long id)
    {
        return appBuildInfoMapper.selectAppBuildInfoById(id);
    }

    /**
     * 查询移动端设计应用信息列表
     *
     * @param appBuildInfo 移动端设计应用信息
     * @return 移动端设计应用信息
     */
    @Override
    public List<AppBuildInfo> selectAppBuildInfoList(AppBuildInfo appBuildInfo)
    {
        return appBuildInfoMapper.selectAppBuildInfoList(appBuildInfo);
    }

    /**
     * 新增移动端设计应用信息
     *
     * @param appBuildInfo 移动端设计应用信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AppBuildInfo insertAppBuildInfo(AppBuildInfo appBuildInfo)
    {
        appBuildInfo.setCreateTime(DateUtils.getNowDate());
        appBuildInfo.setAppKey(UUID.fastUUID().toString().replaceAll("-",""));
        appBuildInfoMapper.insertAppBuildInfo(appBuildInfo);
        /**
         * 新增页面树
         */
        //根节点
        FromBuildPage fromBuildPage = new FromBuildPage();
        fromBuildPage.setAppKey(appBuildInfo.getAppKey());
        fromBuildPage.setStatus("0");
        fromBuildPage.setParentId("0");
        fromBuildPage.setPageName(appBuildInfo.getAppName());
        fromBuildPage.setBuildKey(appBuildInfo.getAppKey());
        fromBuildPage.setNum(1l);
        fromBuildPageMapper.insertFromBuildPage(fromBuildPage);
        //首页
        fromBuildPage = new FromBuildPage();
        fromBuildPage.setAppKey(appBuildInfo.getAppKey());
        fromBuildPage.setStatus("0");
        List<FromBuildPage> fromBuildPages = fromBuildPageMapper.selectFromBuildPageList(fromBuildPage);
        fromBuildPage.setParentId("0");
        fromBuildPage.setPageName("首页");
        String buildKey = UUID.fastUUID().toString().replaceAll("-","");
        fromBuildPage.setBuildKey(buildKey);
        fromBuildPage.setNum(1l);
        fromBuildPageMapper.insertFromBuildPage(fromBuildPage);

        //二级页面
        fromBuildPage = new FromBuildPage();
        fromBuildPage.setBuildKey(buildKey);
        fromBuildPages = fromBuildPageMapper.selectFromBuildPageList(fromBuildPage);
        fromBuildPage = new FromBuildPage();
        fromBuildPage.setAppKey(appBuildInfo.getAppKey());
        fromBuildPage.setStatus("0");
        fromBuildPage.setParentId(fromBuildPages.get(0).getId().toString());
        for (int i = 1; i < 5; i++){
            fromBuildPage.setPageName("二级页面-"+i);
            fromBuildPage.setBuildKey(UUID.fastUUID().toString().replaceAll("-",""));
            fromBuildPage.setNum((long) i);
            fromBuildPageMapper.insertFromBuildPage(fromBuildPage);
        }
        return appBuildInfo;
    }

    /**
     * 修改移动端设计应用信息
     *
     * @param appBuildInfo 移动端设计应用信息
     * @return 结果
     */
    @Override
    public int updateAppBuildInfo(AppBuildInfo appBuildInfo)
    {
        appBuildInfo.setUpdateTime(DateUtils.getNowDate());

        FromBuildPage findPage = new FromBuildPage();
        findPage.setAppKey(appBuildInfo.getAppKey());
        findPage.setStatus("0");
        List<FromBuildPage> fromBuildPages = fromBuildPageMapper.selectFromBuildPageList(findPage);
        FromBuildPage fromBuildPage = fromBuildPages.get(0);
        fromBuildPage.setPageName(appBuildInfo.getAppName());
        fromBuildPageMapper.updateFromBuildPage(fromBuildPage);
        return appBuildInfoMapper.updateAppBuildInfo(appBuildInfo);
    }

    /**
     * 批量删除移动端设计应用信息
     *
     * @param ids 需要删除的移动端设计应用信息主键
     * @return 结果
     */
    @Override
    public int deleteAppBuildInfoByIds(Long[] ids)
    {
        return appBuildInfoMapper.deleteAppBuildInfoByIds(ids);
    }

    /**
     * 删除移动端设计应用信息信息
     *
     * @param id 移动端设计应用信息主键
     * @return 结果
     */
    @Override
    public int deleteAppBuildInfoById(Long id)
    {
        return appBuildInfoMapper.deleteAppBuildInfoById(id);
    }
}
